/*
 *  Copyright (C) 2010 Matthias Buch-Kromann <mbk.isv@cbs.dk>
 * 
 *  This file is part of the MatrixParser package.
 *  
 *  The MatrixParser program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU Lesser General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 * 
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Lesser General Public License for more details.
 * 
 *  You should have received a copy of the GNU Lesser General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package org.osdtsystem.matrixparser.featureextraction;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import org.osdtsystem.matrixparser.data.CONLLSentence;
import org.osdtsystem.matrixparser.parsers.output.DependencyTree;
import org.osdtsystem.matrixparser.features.FeatureVector;
import org.osdtsystem.matrixparser.parsers.ParsingModel;

/**
 *
 * @author Matthias Buch-Kromann <mbk.isv@cbs.dk>
 */
public class HigherOrderMbkRoles extends AbstractHigherOrderExtractor {
    // Genes
    static final String name = "roles";
    static final int genes = 8;

    public HigherOrderMbkRoles(ParsingModel model) {
        super(model, name, genes);
    }

    Set<Integer> roles = new HashSet<Integer>();

    public void extractHigherOrder(FeatureVector fv, DependencyTree tree, int node) {
        CONLLSentence sentence = tree.sentence();
        int gene0 = firstGene();

        // Find node pos and lemma
        String cpostag = sentence.get(node).cpostag();
        String postag = sentence.get(node).postag();
        String lemma = sentence.get(node).lemma();
        if (lemma == null || lemma.equals(""))
            lemma = sentence.get(node).form();

        // Record roles
        roles.clear();
        List<Integer> dependents = new ArrayList<Integer>();
        dependents.addAll(tree.leftChildren(node));
        dependents.addAll(tree.rightChildren(node));
        for (int dependent : dependents) {
            int label = tree.label(dependent);
            if (! roles.contains(label)) {
                // Record as role and elicit features
                roles.add(label);
                String labelName = tree.labelName(dependent);
                if (on(gene0+0)) add(fv, "roles1:R", labelName);
                if (on(gene0+1)) add(fv, "roles1:RC", labelName, cpostag);
                if (on(gene0+2)) add(fv, "roles1:RP", labelName, postag);
                if (on(gene0+3)) add(fv, "roles1:RL", labelName, lemma);
            } else {
                // Elicit features
                String labelName = tree.labelName(dependent);
                if (on(gene0+4)) add(fv, "roles1+:R", labelName);
                if (on(gene0+5)) add(fv, "roles1+:RC", labelName, cpostag);
                if (on(gene0+6)) add(fv, "roles1+:RP", labelName, postag);
                if (on(gene0+7)) add(fv, "roles1+:RL", labelName, lemma);
            }
        }
    }
}
